package signature

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"sort"
	"strings"
)

func calcSignature(data map[string]interface{}, secret_key string) string {
	params := make([]string, 0)
	for key, value := range data {
		params = append(params, fmt.Sprintf("%s=%s", key, value))
	}

	sort.Strings(params)

	h := hmac.New(sha1.New, []byte(secret_key))
	paramsString := base64.StdEncoding.EncodeToString([]byte(strings.Join(params, "&")))
	h.Write([]byte(paramsString))
	hash := h.Sum(nil)

	return hex.EncodeToString(hash[:])
}
